package com.ronglian.ucdisk;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ronglian.ucdisk.share.ShareEvent;
import com.ronglian.ucdisk.share.service.ShareService;
import com.ronglian.ucdisk.share.service.impl.ShareServiceImpl;
import com.ronglian.ucdisk.share.service.impl.TestService;
import com.ronglian.ucdisk.test.Department;
import com.ronglian.ucdisk.test.Employee;

/**
 * Share-0.0.1-SNAPSHOTHello world!
 *
 */

public class App 
{

	
	public static ApplicationContext ctx = null;  
	ShareEvent event  =  null; 
	ShareService service = null; 
	TestService ts = null; 
	public App () {
		 ctx = new ClassPathXmlApplicationContext("application.xml") ;
		 service = (ShareService)ctx.getBean("ShareServiceImpl");
		 ts = (TestService)ctx.getBean("TestService");
	}
	public void run() throws IOException{
		
		 
//		test(); 
//		if(true){
//			return;
//		}
		byte []b = new byte[1024] ; 
		while (true) {
			System.out.println("Please input (delete or add or query or delnode or addnode or sharesTome or info):");
			int n = System.in.read(b);
			String a = new String(b, 0, n - 2);
			if("delete".equals(a))	{
				deleteShare();
			}else if("add".equals(a)) {
				createShare();
			}else if("query".equals(a)){
				queryShare("senderId-123"); 
			}else if("delnode".equals(a)){
				deleteNode();
			}else if("addnode".equals(a)){
				addNode();
			}else if("sharesTome".equals(a)){
				sharesTome();
			}else if("info".equals(a)){
				queryInfo();
			}
			else{
				System.out.println("not delete or add");
			}
		}
	}
	private void queryInfo() {
		ShareEvent event = service.getShareDetail(this.event.id);
		List<HashMap<String,String>> nodeList = event.nodeList ;
		System.out.println("=== Node List ====");
		for(HashMap<String,String> map :nodeList) {
			System.out.println(map.get("id") + "\t" + map.get("name"));
		}
		System.out.println("=== Receiver List === ");
		for(HashMap<String,String> map:event.receiverList ) {
			System.out.println(map.get("id")+"\t"+map.get("name")); 
		}
		System.out.println("=== Receiver Team List ===" );
		if(event.receiverTeamList!=null) {for(HashMap<String,String> map:event.receiverTeamList ) {
			System.out.println(map.get("id")+"\t"+map.get("name")); 
		}}
		
	}
	private void sharesTome() {
		List<ShareEvent> events = service.sharesToMe("r123", 100, 1);
		System.out.println(events.size());
		for(ShareEvent event:events){
			System.out.println("\t" + event.id);
		}
		
	}
	public ShareEvent mock() {
		ShareEvent event = new ShareEvent(); 
	 	event.id  = UUID.randomUUID().toString();
		event.createTime = new Date();
		event.lastUpdateTime = new Date(); 
		event.shareTitle = "Test Share " ; 
		event.endTime = new Date() ; 
		event.type = "in-site";
		event.permission = "view,download" ; 
		
		event.senderId = "senderId-123" ;
		event.setReceiverIdList("r123,r456") ; 
		event.setNodeIdList( "n123,n456" ) ; 
		
		return event ;
	}
	public void createShare() {
		event = mock(); 
		service.createShare( event);
	}
	public void deleteShare() {
		service.deleteSend(event.id);
	}
	public void deleteNode(){
		service.deleteNode(event.id, "n123");
	}
	public void addNode(){
		service.addNode(event.id, "n_add");
	}
	public void queryShare(String userId) {
		List<ShareEvent> events = service.myShares(userId, 10, 1);
		for(ShareEvent event:events) {
			System.out.println("\t" + event.id);
		}
	}

	public void test() 
	{
		ts.test();
		
		
		//service.em.persist(depart);
	}
    public static void main( String[] args ) throws Exception
    {
    	
    	App app = new App(); 
    	app.run();
    }
}
